// 启动服务模块
const express = require('express');
// 连接数据库模块
const mongoose = require('mongoose');
const cors = require('cors');

const { mock, Random } = require('mockjs');

// 各个接口文件
const authRouter = require('./router/authRouter');
const reservation = require('./router/reservation');
const costRouter = require('./router/costRouter');
const doctorRouter = require('./router/doctorRouter');
const home = require('./router/home');

// 连接数据库
mongoose
    .connect('mongodb://127.0.0.1:27017/QWsystem')
    .then(() => {
        console.log('数据库连接成功==>27017');

        // 开启服务

        const app = express();

        // 解析数据格式
        app.use(express.json()); //解析json数据
        app.use(express.urlencoded()); //解析urlencoded数据

        app.use(cors());

        // 配置静态资源文件夹
        app.use(express.static('./static'));

        // 引入接口文件
        app.use('/api/auth', authRouter);
        app.use('/api/booking', reservation);
        app.use('/api/cost', costRouter);
        app.use('/api/doctor', doctorRouter);
        app.use('/api/home', home);

        // 挂载端口
        app.listen(8088, '127.0.0.1', (error) => {
            if (error) {
                console.log(error);
                return;
            }
            console.log('服务器启动成功:端口号8088');
        });

        // 调用方法插入假数据
        // test()
        // 预约挂号假数据
        // booking()
        // 患者信息假数据
        // orderdata();
        // 就诊信息假数据
        // visit();
        // 消费信息假数据
        // costdata();
        // 医生管理信息数据
        // doctor();
        // 就诊项目数据
        // progetto();
    })
    .catch(() => {
        console.log('数据库连接失败...');
    });

// 预约挂号数据
async function booking() {
    const Reservation = require('./model/Reservation');
    const data = mock({
        'list|30': [
            {
                name: '@cname',
                'bookingID|1-100': 1,
                'cardType|1': ['居民身份证', '港澳通行证', '护照'],
                IDcard: /^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(10|11|12))(([012][1-9])|3[01])\d{3}[0-9Xx]$/,
                tel: /^1(3|4|5|6|7|8|9)\d{9}$/,
                'department|1': [
                    '内科-肾内科',
                    '内科-消化内科',
                    '内科-血液内科',
                    '外科-肠胃外科',
                    '外科-创伤外科',
                ],
                'bookingType|1': ['专家号'],
                'cost|100-10000': 100,
                'status|1': ['待就诊', '已就诊', '已取消'],
                'bookingDate|1': [
                    1691325891822, 1691325891822, 1691498691822, 1691585091822,
                    1691671491822, 1691757891822,
                ],
                // waitingTime: 1691487654305,
                // bookingTime: 1691487654305
                'hospital|1': [
                    '广州市第一人民医院',
                    '广州市第二人民医院',
                    '广州市第三人民医院',
                    '广州市中医院',
                ],
                'address|1': '广州市xx区xxx',
                'doctor|1': ['张三', '李四', '王五', '赵六'],
                'doctorType|1': ['主任医师', '副主任医师'],
            },
        ],
    });
    await Promise.all(data.list.map((item) => Reservation.create(item)));
    console.log('预约挂号数据插入成功');
}

// 消费记录假数据
async function costdata() {
    const CostRecords = require('./model/CostRecords');
    const data = mock({
        'list|30': [
            {
                'time|1': [
                    Date.now() - 100000,
                    Date.now() - 900000,
                    Date.now() - 1000000,
                    Date.now() - 5000000,
                    Date.now() - 6000000,
                ],
                'visitUserID|1': [
                    '64fa6de9edb29d1447e3a659',
                    '64fa6de9edb29d1447e3a65a',
                    '64fa6de9edb29d1447e3a65b',
                    '64fa6de9edb29d1447e3a65f',
                    '64fa6de9edb29d1447e3a662',
                ],
                'visitObjID|1': [
                    '64fa6dde10923bb8cc48dd2f',
                    '64fa6dde10923bb8cc48dd30',
                    '64fa6dde10923bb8cc48dd31',
                    '64fa6dde10923bb8cc48dd36',
                    '64fa6dde10923bb8cc48dd2d',
                ],
                orderId: 2020081900098,
                'from|1': ['微信小程序', '七尾医疗App', '线下就诊'],
                'uocost|1': ['余额', '微信支付', '支付宝', '现金'],
                'Paymentstatus|1': ['已缴费', '未缴费', '缴费异常'],
                'Department|1': [
                    '内科/肾内科',
                    '外科/牙科',
                    '内科/神经科',
                    '外科/皮肤科',
                ],
            },
        ],
    });
    data.list = data.list.map((item, index) => {
        item.orderId = 'JF' + (item.orderId + index);
        return item;
    });
    await Promise.all(data.list.map((item) => CostRecords.create(item)));
    console.log('消费记录假数据');
}

// 患者信息假数据
async function orderdata() {
    const order = require('./model/Order');
    const data = mock({
        'list|10': [
            {
                tel: /^1(3|4|5|6|7|8|9)\d{9}$/,
                'avatar|1': [
                    'http://127.0.0.1:8088/default-avatar.jpg',
                    'http://127.0.0.1:8088/default-avatar2.jpg',
                    'http://127.0.0.1:8088/default-avatar3.png',
                ],
                username: '@cname',
                wxname: '@cname',
                visitID: /^5(3|4|5|6|7|8|9)\d{10}$/,
                'IDcardtype|1': ['身份证件', '工作证件', '结婚证件'],
                IDcartnumber:
                    /^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(10|11|12))(([012][1-9])|3[01])\d{3}[0-9Xx]$/,
                'address|1': [
                    '北京市朝阳区**路**号**小区',
                    '广州市天河区**路**号**小区',
                    '广州市黄埔区**路**号**小区',
                ],
                'about|1': ['本人', '家属', '同事', '123'],
                'balance|1': [1000, 50, 511, 456, 100000],
            },
        ],
    });
    await Promise.all(data.list.map((item) => order.create(item)));
    console.log('患者信息假数据');
}

// 就诊信息表假数据
async function visit() {
    const visit = require('./model/VisitData');
    const data = mock({
        'list|10': [
            {
                'Hospital|1': [
                    '北京市第一人民医院/东院',
                    '广州市第一人民医院/东院',
                    '广州市第二人民医院/西院',
                ],
                'Hospitaladdress|1': [
                    '北京市大兴区**路990号',
                    '广州市天河区**路100号',
                    '广州市黄埔区**路200号',
                ],
                'Department|1': [
                    '消化内科-门诊3楼东区501',
                    '牙科-门诊1楼东区501',
                    '神经科-门诊5楼北区501',
                    '外科-门诊2楼南区401',
                ],
                Doctor: '@cname',
                'Doctorjob|1': ['主任医师', '医科主任', '主科医生'],
                'Business|1': ['门诊缴费', '购药缴费', '住院缴费'],
                'Costtype|1': ['西药费', '中药费', '住院费'],
                'address|1': [
                    [
                        {
                            entryname: '普罗布考片',
                            price: 10,
                            quantity: 1,
                            total: 10,
                        },
                        {
                            entryname: '阿司匹林肠溶片',
                            price: 15,
                            quantity: 1,
                            total: 15,
                        },
                        {
                            entryname: '普罗布考片',
                            price: 10,
                            quantity: 1,
                            total: 10,
                        },
                        {
                            entryname: '阿司匹林肠溶片',
                            price: 15,
                            quantity: 1,
                            total: 15,
                        },
                    ],
                    [
                        {
                            entryname: '黄芪',
                            price: 20,
                            quantity: 1,
                            total: 20,
                        },
                        { entryname: '生姜', price: 5, quantity: 1, total: 5 },
                        {
                            entryname: '白茅根',
                            price: 8,
                            quantity: 1,
                            total: 8,
                        },
                        {
                            entryname: '薏苡仁',
                            price: 12,
                            quantity: 1,
                            total: 12,
                        },
                    ],
                    [
                        {
                            entryname: '重症科',
                            price: 1000,
                            quantity: 1,
                            total: 1000,
                        },
                        {
                            entryname: 'ICU',
                            price: 2000,
                            quantity: 1,
                            total: 2000,
                        },
                        {
                            entryname: '轻症科',
                            price: 500,
                            quantity: 1,
                            total: 500,
                        },
                        {
                            entryname: '骨科',
                            price: 1500,
                            quantity: 1,
                            total: 1500,
                        },
                    ],
                ],
            },
        ],
    });
    await Promise.all(data.list.map((item) => visit.create(item)));
    console.log('就诊信息表假数据');
}

// 医生管理信息
async function doctor() {
    const doctor = require('./model/DoctorData');
    const data = mock({
        'list|30': [
            {
                Doctorname: '秦超',
                'avatar|1': [
                    'http://127.0.0.1:8088/default-avatar.jpg',
                    'http://127.0.0.1:8088/default-avatar2.jpg',
                    'http://127.0.0.1:8088/default-avatar3.png',
                ],
                'Courtyardarea|1': ['东院', '西院', '南院', '北院'],
                'Doctorjob|1': ['主任医师', '医科主任', '主科医生'],
                'Department|1': ['内科-肾内科'],
                Tags: ['糖尿病', '尿毒症', '肾病'],
                Doctorcontent: () => Random.cparagraph(),
                'time|1': [
                    Date.now() - 100000,
                    Date.now() - 900000,
                    Date.now() - 1000000,
                    Date.now() - 5000000,
                    Date.now() - 6000000,
                ],
            },
        ],
    });

    await Promise.all(data.list.map((item) => doctor.create(item)));
    console.log('医生管理信息');
}

async function progetto() {
    const progetto = require('./model/progetto');
    const data = mock({
        'list|12': [
            {
                'entryname|+1': [
                    '普罗布考片',
                    '阿司匹林肠溶片',
                    '退烧药',
                    '感冒药',
                    '黄芪',
                    '生姜',
                    '白茅根',
                    '薏苡仁',
                    '重症科',
                    'ICU',
                    '轻症科',
                    '骨科',
                ],
                'price|1-3000': 1,
            },
        ],
    });
    await Promise.all(data.list.map((item) => progetto.create(item)));
    console.log('就诊项目');
}
